package com.application.lucky.activity.me;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.jsonBean.MoneyHistoryBean;
import com.application.lucky.config.Config;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.qunar.im.ui.util.MoneyUtils;
import com.application.lucky.widget.TitlebarView;
import com.application.lucky.widget.dialog.MoneyHistoryDialog;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.DateUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

public class MoneyHistoryActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private RelativeLayout mRlSelect;
    private TextView mTvSelectType;
    private RecyclerView mRclView;
    private MoneyHistoryDialog dialog;
    private MyAdapter myAdaopter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_money_history;
    }

    private void initView() {
        mTitleBar = findViewById(R.id.title_bar);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        mRlSelect = findViewById(R.id.rl_select);
        mRlSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog == null)
                    dialog = new MoneyHistoryDialog(MoneyHistoryActivity.this, new MoneyHistoryDialog.Select() {
                        @Override
                        public void select(String s, int p) {
                            mTvSelectType.setText(s);
                            index = 0;
                            if (p != -1) {

                                type = types[p];
                            } else {
                                type = -1;
                            }
                            loadData();
                        }
                    });

                dialog.show();

            }
        });
        mTvSelectType = findViewById(R.id.tv_select_type);
        mRclView = findViewById(R.id.rcl_view);
        mRclView.setLayoutManager(new LinearLayoutManager(this));
        myAdaopter = new MyAdapter(R.layout.item_money_history);
        myAdaopter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                index += Config.length;
                loadData();
            }
        }, mRclView);
        mRclView.setAdapter(myAdaopter);
        myAdaopter.setEmptyView(R.layout.empty_view);
        loadData();

    }

    private int index = 0;
    public static final int DEF = 0;
    public static final int TYPE_RECHARGE = 1;
    public static final int TYPE_SEND_RED = 2;
    public static final int TYPE_TICHENG = 3;
    public static final int TYPE_ZHUANZHANG = 4;
    public static final int TYPE_YONGJIN = 5;
    public static final int TYPE_TIQU = 6;
    int[] types = {0, 2, 6, 1, 3, 4};

    int type = 0;

    private void loadData() {
        showloadDialog();
        new RedPRequest().red_user_balance_history(index, Config.length, type, new CallBackEx<BaseResponse<MoneyHistoryBean>>() {
            @Override
            public void onFailed(int code) {
                if (isFinishing()) {
                    return;
                }
                hideLoadDialog();
                myAdaopter.loadMoreFail();
            }

            @Override
            public void onSuccess(BaseResponse<MoneyHistoryBean> moneyHistoryBeanBaseResponse) {
                if (isFinishing()) {
                    return;
                }
                hideLoadDialog();
                MoneyHistoryBean dataInfo = moneyHistoryBeanBaseResponse.dataInfo;
                if (dataInfo != null) {
                    List<MoneyHistoryBean.UserBalanceChangeInfoBean> list = dataInfo.list;
                    if (list != null) {
                        if (index == 0) {
                            myAdaopter.setNewData(list);

                        } else {
                            myAdaopter.addData(list);
                        }
                        if (list.size() < Config.length) {
                            myAdaopter.loadMoreEnd(true);
                        } else {
                            myAdaopter.loadMoreComplete();
                        }
                    }


                }
            }
        });
    }

    class MyAdapter extends BaseQuickAdapter<MoneyHistoryBean.UserBalanceChangeInfoBean, BaseViewHolder> {

        /**
         * <item>全部</item>0
         * <item>红包</item>2
         * <item>提现</item>6
         * <item>充值</item>1
         * <item>退款</item>5
         * <item>佣金</item>3
         * <item>转账</item>4
         * <p>
         * int[] types = {0, 2, 6, 1, 3, 4};
         *
         * @param layoutResId
         */
        public MyAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, MoneyHistoryBean.UserBalanceChangeInfoBean item) {
            String name = "未知";
            int res = R.drawable.icon_history_huodong;

            switch (item.getChangeType()) {

                case 2:
                    name = "红包";
                    res = R.drawable.icon_history_hongbao;
                    break;
                case 6:
                    name = "提现";
                    res = R.drawable.icon_history_tixian;
                    break;
                case 1:
                    name = "充值";
                    res = R.drawable.icon_history_chongzhi;
                    break;
                case 3:
                    name = "佣金";
                    res = R.drawable.icon_history_yongjin;
                    break;
                case 4:
                    name = "转账";
                    res = R.drawable.icon_history_zhuanzhang;
                    break;
                case 5:
                    name="转账退款";
                    res = R.drawable.icon_history_zhuanzhang;
                    break;
            }
            String add;
            if (item.getIsAdd() == 2) {
                add = "-";
            } else {
                add = "+";
            }
            String m;
            if (item.getChangeType() == 3) {
                m = item.getBalance() + "";
            } else {
                m = MoneyUtils.getMoney(item.getBalance());
            }
            helper.setText(R.id.iv_money_history_list_name, name)
                    .setImageResource(R.id.iv_money_history_head, res)
                    .setText(R.id.iv_money_history_money, add + m)
                    .setText(R.id.iv_money_history_time, DateUtils.getRedTime(item.getCreateTime()));
        }
    }
}
